package com.infitecs.eyas.quality;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.judge.service.JudgeService;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.ElementService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by Demon on 2015/4/24.
 */
@Controller
@RequestMapping("/quality/element/")
public class ElementController {
    @Autowired
    private ElementService elementService;
    @Autowired
    private StepService stepService;
    @Autowired
    private JudgeService judgeService;

    @RequestMapping(value = "calculateSum",method = RequestMethod.POST)
    @ResponseBody
    public double calculateSum(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
        return elementService.calculateSumByFormula6(caseId);
    }

    @RequestMapping(value = "calculateQnetArOther")
    @ResponseBody
    public double calculateQnetArOther(HttpSession session){
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
        return elementService.calculateQnetArOtherByFormula2(caseId);
    }

    @RequestMapping(value = "calculateQgrArOther")
    @ResponseBody
    public double calculateQgrArOther(HttpSession session){
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
        return elementService.calculateQgrArOtherByFormula4(caseId);
    }

    @RequestMapping(value = "calculateQnetArAndQnetArOther",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateQnetArAndQnetArOther(HttpSession session){
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Double qnetAr = elementService.calculateQnetArByFormula1(caseId);
        Double qnetArOther = elementService.calculateQnetArOtherByFormula2(caseId);
        AjaxResponse response = new AjaxResponse();
        response.addAttribute("qnetAr",qnetAr);
        response.addAttribute("qnetArOther",qnetArOther);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
        return response;
    }

    @RequestMapping(value = "calculateQgrArAndQgrArOther", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateQgrArAndQgrArOther(HttpSession session){
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Double qgrAr = elementService.calculateQgrArByFormula3(caseId);
        Double qgrArOther = elementService.calculateQgrArOtherByFormula4(caseId);
        AjaxResponse response = new AjaxResponse();
        response.addAttribute("qgrAr",qgrAr);
        response.addAttribute("qgrArOther",qgrArOther);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
        return response;
    }

    @RequestMapping(value = "save",method = {RequestMethod.GET,RequestMethod.POST})
    public String save(HttpSession session){
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        stepService.updateStepStateFinish(caseId,Constant.STEP_ID_2);
        return "redirect:/quality/grey?menuId=10";
    }

    @RequestMapping(value = "updateCar",method = RequestMethod.POST)
    @ResponseBody
    public void updateCar(@RequestParam("car")double car,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateCar(car,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateHar",method = RequestMethod.POST)
    @ResponseBody
    public void updateHar(@RequestParam("har")double har,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateHar(har,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateOar",method = RequestMethod.POST)
    @ResponseBody
    public void updateOar(@RequestParam("oar")double oar,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateOar(oar,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateNar",method = RequestMethod.POST)
    @ResponseBody
    public void updateNar(@RequestParam("nar")double nar,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateNar(nar,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateSar",method = RequestMethod.POST)
    @ResponseBody
    public void updateSar(@RequestParam("sar")double sar,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateSar(sar,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateQnetAr",method = RequestMethod.POST)
    @ResponseBody
    public void updateQnetAr(@RequestParam("qnetAr")double qnetAr,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateQnetAr(qnetAr,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateQnetArOther",method = RequestMethod.POST)
    @ResponseBody
    public void updateQnetArOther(@RequestParam("qnetArOther")double qnetArOther,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateQnetArOther(qnetArOther,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateQgrAr",method = RequestMethod.POST)
    @ResponseBody
    public void updateQgrAr(@RequestParam("qgrAr")double qgrAr,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateQgrAr(qgrAr,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "updateQgrArOther",method = RequestMethod.POST)
    @ResponseBody
    public void updateQgrArOther(@RequestParam("qgrArOther")double qgrArOther,HttpSession httpSession) {
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        elementService.updateQgrArOther(qgrArOther,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_2);
    }

    @RequestMapping(value = "recover",method = {RequestMethod.GET})
    @ResponseBody
    public Element getElement(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        return elementService.getElement(caseId);
    }

    @RequestMapping(value = "elementSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse elementSave(Element element) {
        try {
            elementService.elementSave(element);
            return new AjaxResponse();
        } catch (Exception e) {
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
